$(function () {
    processingCheckIn();
    $("body").on('click','#calendarBtn,#calendarBtn1',function () {
        querySignInList();
    });
});
var totalNum = 0;
function saveSignIn(index) {
    var urls = baseloaction+"/web/uc/ajax/saveSignIn";
    $.ajax({
        url: urls,
        type: "post",
        dataType: "json",
        success: function(result) {
            if(result.success==true){
                icModale(0, "", result.message, null,null,null);
                $(".processing-check-in").hide();
                $("#calendarBtn1").html('<i class="fa fa-calendar-check-o mr5"></i>已签到');
                setText('signNum',totalNum + 1,'总签到数:'+(totalNum + 1)+'天');
                layer.close(index);
            }else{
                icModale(0, "", result.message, null,null,null);
            }
        },
        error: function(error){
            console.info("-----获取签到数据错误");
        }
    });
}

/**
 * 查询签到数据
 */
function querySignInList() {
    var signList = null;
    var obj = getData(null,null);
    var totalSignNum = 0;
    var mothsNum = 0;
    if(obj != null){
        signList = obj.signList;
        totalSignNum = obj.signTotalNum;
        totalNum = totalSignNum;
        mothsNum = obj.mothsSignNum;
        isSign = obj.isSign;
    }
    var context ='<div class="sTitle">'+
        '<span class="r">累计签到 <big id="totalSignNum">'+totalSignNum+'</big> 天</span>'+
        '<strong class="vam">本月签到</strong>'+
        '<strong class="vam"><big id="mothsNum"> '+mothsNum+' </big></strong>'+
        '<strong class="vam">天</strong>'+
        '</div>'+
        '<div class="calendar-wrap" id="calendar"></div>';
    var index = layer.alert(context, {
        type: 1,
        title: false,
        area: ['520px', '580px'],
        closeBtn: 1,
        offset: 'auto',
        shadeClose: true,
        btn : !isSign ? ' 今日签到 ':'已签到',
        btnAlign : 'c',
        skin: 'customCalendar customCalendar__wrap',
        success:function () {
            var sginStatus = $(".layui-layer-btn0").text();
            if (sginStatus == "已签到"){
                $(".layui-layer-btn0").attr("disabled", true);
                $(".layui-layer-btn0").css("pointer-events","none");
            }
            calUtil.init({
                id: "calendar",
                signList: signList,
                disablePrev: false,
                disableNext: false,
                selectPreNextFunction:function (showYear,showMonth) {
                    obj = getData(showYear,showMonth);
                    $("#totalSignNum").text(0);
                    $("#mothsNum").text(0);
                    if(obj !=null){
                        signList = obj.signList;
                        $("#totalSignNum").text(obj.signTotalNum);
                        $("#mothsNum").text(obj.mothsSignNum);
                    }
                    //重新设置选中数据
                    calUtil.signList  = signList;
                }
            });
        },
        yes: function () {
            saveSignIn(index);
        }
    });
}

/***
 * 获取签到数据
 * @param showYear 年份
 * @param showMonth 月份
 * @return {*}
 */
function getData(showYear,showMonth) {
    if(showYear==null){
        showYear ='';
    }
    if(showMonth==null){
        showMonth = '';
    }
    var data = null;
    $.ajax({
        url:baseloaction +'/web/uc/ajax/querySignIn',
        type:'post',
        dataType:'json',
        async:false,
        data:{
            'year':showYear,
            'month':showMonth
        },
        success:function (result) {
            if(result.success==true){
                data = result.entity;
            }
        }
    });
    return data;
}



/**
 * 处理签到
 */
function processingCheckIn() {
    $.ajax({
        url: baseloaction + "/web/uc/ajax/processing/check/in",
        type: "post",
        dataType: "json",
        success: function (result) {
            if(result.success==true){
                if(result.entity>0){
                    $(".processing-check-in").hide();
                    $("#calendarBtn1").html('<i class="fa fa-calendar-check-o mr5"></i>已签到');
                }else{
                    $(".processing-check-in").show();
                    $("#calendarBtn1").html('<i class="fa fa-calendar mr5"></i>签到');
                }
            }
        },
        error: function (error) {
            console.info("-----获取签到数据错误");
        }
    });
}


